<?php
/**
 * 会员中心连接类
 * stourmemberclient.php
 * Author: netman
 * DateTime:  2023/11/27 11:42
 */


class Model_Zhilvyun_Api_Stourmemberclient
{

    private static $serviceUrl = "https://member.stourweb.cn";
    private static $clientWebsiteId = "39";
    private static $clientWebsiteSecret = "99945f6452047599f1a6f088d78b83a8";
    private static $clientTimeout = 0;
    /**
     * @function 远程请求
     * @param $url
     * @param  string  $postfields
     * @param  string  $method
     * @param  array  $headers
     * @return mixed
     */
    private static function http($url, $postfields = '', $method = 'GET')
    {
        if (strpos($url, 'http') === false) {
            return;
        }

        $ci = curl_init();
        $user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
        curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);// 当遇到301/302 直接抓取页面
        curl_setopt($ci, CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        if ($method == 'POST') {
            curl_setopt($ci, CURLOPT_POST, true);
            if ($postfields != '') {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
        }

        curl_setopt($ci, CURLOPT_URL, $url);
        $response = curl_exec($ci);
        curl_close($ci);
        return $response;
    }

    private static function createSign($url, array $query_strings)
    {
        $seed = $url;
        foreach ($query_strings as $key => $query_string) {
            if ($key != "sign") {
                $seed .= $query_string;
            }
        }

        return md5($seed.self::$clientWebsiteSecret);
    }



    /**
     * 通过会员id获取会员信息
     * @param $member_id
     * @return bool
     */
    public static function getMemberInfoById($member_id)
    {

        $api_url = self::$serviceUrl."/api/v2/member/info";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['member_id'] = strval($member_id);
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $rtn = self::http($api_url, $query_strings, 'POST');
        $result = json_decode($rtn, true);
        if ($result && $result['status'] == 1) {
            return $result['data'];
        } else {
            return false;
        }
    }

    /**
     * 获取会员所属企业列表
     * @param $member_id
     * @return false|mixed
     */
    public static function getMemberEnterprise($member_id)
    {

        $api_url = self::$serviceUrl."/api/v2/enterprise/my_enterprise";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['member_id'] = strval($member_id);
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $rtn = self::http($api_url, $query_strings, 'POST');
        $result = json_decode($rtn, true);
        if ($result['status']) {
            return $result['data'];
        } else {
            return false;
        }
    }

    /**
     * 获取会员所属企业id字符串及默认企业id
     */
    public static function getMemberEnterpriseIds($member_id)
    {
        $res = self::getMemberEnterprise($member_id);
        if ($res) {
            $default_enterprise_id = '';
            $enterprise_ids = [];
            foreach ($res as $item) {
                if ($item['is_default'] == 1) {
                    $default_enterprise_id = $item['id'];
                }
                array_push($enterprise_ids, $item['id']);
            }
            return [
                'default_enterprise_id' => $default_enterprise_id,
                'enterprise_ids' => implode(',', $enterprise_ids),
            ];
        } else {
            return false;
        }
    }

    /**
     * 获取企业信息
     * @param $enterprise_id
     * @return mixed
     */
    public static function getEnterpriseEmployee($enterprise_id)
    {
        $get_login_member_url = self::$serviceUrl."/api/enterprise/employee/";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['enterprise_id'] = $enterprise_id;
        $query_strings["sign"] = self::createSign($get_login_member_url, $query_strings);
        $get_login_member_url = $get_login_member_url.'?'.http_build_query($query_strings);
        $result = json_decode(self::http($get_login_member_url), true);
        if ($result && $result['status'] == 1) {
            return $result['data'];
        } else {
            return [];
        }
    }


    //获取企业信息
    public static function getEnterprise($enterprise_id)
    {
        self::setConfig();
        $get_login_member_url = self::$serviceUrl."/api/enterprise/info";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['enterprise_id'] = $enterprise_id;
        $query_strings["sign"] = self::createSign($get_login_member_url, $query_strings);
        $get_login_member_url = $get_login_member_url.'?'.http_build_query($query_strings);
        $result = json_decode(self::http($get_login_member_url), true);
        if ($result && $result['status'] == 1) {
            return $result['data'];
        }
    }



    /**
     * 通过手机号获取会员信息
     * @param $mobile
     * @return false|mixed
     * @author yanqin
     */
    public static function getMemberByMobile($mobile)
    {

        $api_url = self::$serviceUrl."/api/v2/member/info";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['mobile'] = strval($mobile);
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $rtn = self::http($api_url, $query_strings, 'POST');
        $result = json_decode($rtn, true);
        if ($result && $result['status'] == 1) {
            return $result['data'];
        } else {
            return false;
        }
    }

    /**
     * 通过手机号创建会员
     * @param $mobile
     * @return mixed
     * @author yanqin
     */
    public static function createMemberByMobile($mobile)
    {
        $api_url = self::$serviceUrl."/api/v2/member/create_member";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['mobile'] = strval($mobile);
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $rtn = self::http($api_url, $query_strings, 'POST');
        $result = json_decode($rtn, true);
        return $result;

    }

    /**
     * 绑定第三方登录信息
     * @param  array  $data
     * @return bool
     * @author yanqin
     */
    public static function bindThird(array $data): bool
    {

        $api_url = self::$serviceUrl."/api/v2/member/bind_third";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings = array_merge($query_strings, $data);
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $rtn = self::http($api_url, $query_strings, 'POST');
        $result = json_decode($rtn, true);
        if ($result && $result['status'] == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取默认企业名称
     * @param $stourMemberId  // 商家中心会员ID
     * @return string
     * @author yanqin
     */
    public static function getDefaultEnterpriseTitle($stourMemberId): string
    {
        return '默认企业_'.($stourMemberId + 6180);
    }

    //创建企业
    public static function createEnterprise($memberId, $name, $province, $city)
    {
        $api_url = self::$serviceUrl . "/api/enterprise/create/";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['member_id'] = $memberId;
        $query_strings['name'] = $name;
        $query_strings['province'] = $province;
        $query_strings['city'] = $city;
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $api_url = $api_url . '?' . http_build_query($query_strings);
        $rtn = self::http($api_url);
        $result = json_decode($rtn);
        if ($result && $result->status == 1) {
            return ['status' => 1, 'enterprise_id' => $result->data];
        } else {
            return ['status' => 0, 'msg' => $result->msg];
        }
    }

    // 设置默认企业
    public static function setDefaultEnterprise($membeId,$enterpriseId)
    {

        $api_url = self::$serviceUrl . "/api/v2/enterprise/set_default_enterprise";
        $query_strings = ["client_id" => self::$clientWebsiteId];
        $query_strings['member_id'] = $membeId;
        $query_strings['ent_id'] = $enterpriseId;
        $query_strings["sign"] = self::createSign($api_url, $query_strings);
        $rtn = self::http($api_url, $query_strings, 'POST');
        $result = json_decode($rtn);
        if ($result && $result->status) {
            return true;
        } else {
            return false;
        }
    }
}

